<template>
  <div class="parking-interface">

    <el-tabs v-model="activeTab" tab-position="top" type="border-card" class="tab-container">
      <!-- 车位展示 -->
      <el-tab-pane name="parkingDisplay">
        <template #label>
          <span>车位展示</span>
        </template>
        <div class="content-container">
          <div class="zone-selector">
            <span style="color: black;">选择分区：</span>
            <el-select v-model="selectedZone" placeholder="选择分区" @change="onZoneChange" class="zone-select">
              <el-option v-for="zone in zones" :key="zone" :label="zone" :value="zone"></el-option>
            </el-select>
            <div class="scrolling-message"><span>这里是车位选择，点击绿色小车即可购买或者租赁！</span></div>
          </div>
          <div class="left-right">
            <div class="llef"></div>
            <div class="parking-matrix">
              <car3 v-for="(slot, index) in parkingSlots" :key="index" :fill="getFillColor(slot.status)" @click="handleSlotClick(slot)"></car3>
            </div>
            <div class="legend">
              <span><el-badge value="空闲" color="#4caf50" :offset="[13,13]">
                <template #default><car3 fill="#4caf50"></car3></template>
              </el-badge></span>
              <span><el-badge value="已购买" color="#f44336" :offset="[13,13]">
                <template #default><car3 fill="#f44336"></car3></template>
              </el-badge></span>
              <span><el-badge value="已租赁" color="#ff9800" :offset="[13,13]">
                <template #default><car3 fill="#ff9800"></car3></template>
              </el-badge></span>
            </div>
          </div>
        </div>
      </el-tab-pane>
      
      <!-- 我的车位 -->
      <el-tab-pane name="myParking">
        <template #label>
          <span>我的车位</span>
        </template>
        <div class="content-container">
          <el-collapse v-model="activeNames">
            <el-collapse-item title="已购买" name="purchased">
              <div class="card-container">
                <el-card v-for="slot in myPurchasedSlots" :key="slot.id" class="parking-card">
                  <div slot="header" class="clearfix">
                    <span>{{ slot.location }}区 - 车位{{ slot.spaceNumber }}</span>
                  </div>
                  <div>购买日期: {{ slot.startTime }}</div>
                  <!-- <div>价格: {{ slot.price }}</div> -->
                </el-card>
              </div>
            </el-collapse-item>
            <el-collapse-item title="已租赁" name="rented">
              <div class="card-container">
                <el-card v-for="slot in myRentedSlots" :key="slot.id" class="parking-card">
                  <div slot="header" class="clearfix">
                    <span>{{ slot.location }}区 - 车位{{ slot.spaceNumber }}</span>
                  </div>
                  <div>租赁日期: {{ slot.startTime }}</div>
                  <div>截止日期: {{ slot.endTime }}</div>
                </el-card>
              </div>
            </el-collapse-item>
          </el-collapse>
        </div>
      </el-tab-pane>
    </el-tabs>    

    <!-- 购买/租赁确认对话框 -->
    <el-dialog v-model="dialogVisible" title="确认购买/租赁">
      <div>是否确认购买或租赁这个车位？</div>
      <template #footer>
        <span  class="dialog-footer">
          <el-button type="primary" @click="openFormDialog">确认</el-button>
          <el-button @click="dialogVisible = false">取消</el-button>
        </span>
      </template>
    </el-dialog>
    
    <!-- 购买/租赁信息填写表单 -->
    <el-dialog v-model="formDialogVisible" title="填写信息" class="el-dialog">
      <el-form :model="form" :rules="rules" ref="formRef">
        <el-form-item label="购买或租赁" prop="action">
          <el-select v-model="form.action" placeholder="请选择">
            <el-option label="购买" value="purchase"></el-option>
            <el-option label="租赁" value="rent"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.action === 'purchase'" label="价格" prop="price">
          <el-input v-model="form.price"  type="number" disabled></el-input>
        </el-form-item>
        <el-form-item v-if="form.action === 'rent'" label="租期 (月)" prop="duration">
          <el-input v-model="form.duration" type="number" min="1"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="formDialogVisible = false">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import car3 from '@/components/iconBywzy/car3.vue';
import { Path } from "@/constant/index"
import { get,post } from "@/plugins/axios"
const activeTab = ref('parkingDisplay');
const selectedZone = ref('A');
const zones = ['A', 'B', 'C', 'D'];
const parkingSlots = ref([]);
const myPurchasedSlots = ref([]);
const myRentedSlots = ref([]);

getMySpace()
// 待登入功能加入后需要去掉这个参数
function getMySpace(){
  get(Path.parkingSpaceGetMySpace,null,content =>{
    myPurchasedSlots.value = content.myPurchasedSlots
    myRentedSlots.value = content.myRentedSlots
  })
}

const dialogVisible = ref(false);
const formDialogVisible = ref(false);
const selectedSlot = ref(null);

// 设置月租金
const rentMonth = 200;

const form = reactive({
  action: null,
  price: 100000,
  duration: 1,
  rent: 0
});

const rules = {
  action: [{ required: true, message: '请选择购买或租赁', trigger: 'change' }],
  price: [{ required: true, message: '请输入价格', trigger: 'blur', type: 'number' }],
  duration: [{ required: true, message: '请输入租期', trigger: 'blur', type: 'number' }],
};

const activeNames = ref(['purchased', 'rented']);

const generateParkingSlots = () => {
  parkingSlots.value = [];
  let carSpace = ref([])
  get(Path.parkingSpaceList,{targetSpace: selectedZone.value},content => {
    carSpace.value = content
    for (let i = 0; i < 40; i++) {
    const status = carSpace.value[i].state;
    parkingSlots.value.push({
      id: carSpace.value[i].id,
      zone: selectedZone.value,
      status,
    });
  }
  })
};

const getFillColor = (status) => {
  switch (status) {
    case 0:
      return '#4caf50'; // 绿色
    case 2:
      return '#f44336'; // 红色
    case 1:
      return '#ff9800'; // 橙色
    default:
      return '#000000'; // 默认黑色
  }
};

const onZoneChange = () => {
  generateParkingSlots();
};

const handleSlotClick = (slot) => {
  selectedSlot.value = slot;
  if (slot.status === 0) {
    dialogVisible.value = true;
  } else if (slot.status === 2) {
    ElMessage.error('该车位已被购买');
  } else if (slot.status === 1) {
    ElMessage.error('该车位已被租赁');
  }
};

const openFormDialog = () => {
  dialogVisible.value = false;
  formDialogVisible.value = true;
};

// 提交表单 购买或租赁车位
const submitForm = () => {
  ElMessageBox.confirm('提交后不可更改，请确认信息无误！', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    selectedSlot.value.status = form.action === 'purchase' ? 2 : 1;
    // 在这里将表单提交给后端 来进行状态和时间的修改和所属用户ID的修改
    if(form.action != null){
      const parma = {
        id: selectedSlot.value.id,
        state: selectedSlot.value.status,
        begin: new Date().getTime(),
    }
    if(form.action === 'purchase' ){
      parma.end = new Date().getTime() + 2207520000000
      parma.price = 100000
    } else {
      parma.end = new Date().getTime() + form.duration * 2592000000
      parma.price = ( form.duration * rentMonth)
    }
    post(Path.parkingSpaceChangeState,parma,content => {
        generateParkingSlots()
      })
    formDialogVisible.value = false;
    resetForm();
     setTimeout(function() {  
        location.reload();
    }, 1000); 
    } else {
    ElMessage.warning('请进行选择');
    }
  }).catch(() => {
    ElMessage.info('已取消');
  });
};

const resetForm = () => {
  form.name = '';
  form.contact = '';
  form.action = '';
  form.price = 0;
  form.duration = 0;
};

onZoneChange();
</script>

<style scoped>
.parking-interface {
  height: auto;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: linear-gradient(135deg, #2c3e50, #fd746c);
  padding: 0px;
  border-radius: 10px;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
  color: #fff;
}

.tab-container {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 10px;
  backdrop-filter: blur(10px);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
}

.content-container {
  padding: 20px;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 10px;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
}

.zone-selector {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.zone-select {
  margin-left: 10px;
  width: 120px;
}

.scrolling-message {
  margin-left: auto;
  margin-right: 40%;
  overflow: hidden;
  white-space: nowrap;
  margin-bottom: 20px;
  position: relative;
  padding: 10px 0;
}

.scrolling-message span {
  display: inline-block;
  animation: scroll-left 10s linear infinite;
  font-size: 1.2em;
  font-weight: bold;
  color: #fff;
  background: linear-gradient(90deg, #ff4081, #81d4fa, #ff4081);
  background-size: 200%;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
}

@keyframes scroll-left {
  0% {
    transform: translateX(40%);
  }
  100% {
    transform: translateX(-100%);
  }
}

.left-right {
  display: flex;
}

.llef {
  flex: 1;
}

.legend {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-content: center;
  align-items: center;
  margin-top: 10px;
  margin-bottom: 20px;
}

.legend span {
  margin: 5px 0;
}

.parking-matrix {
  flex: 4;
  display: grid;
  width: 60%;
  grid-template-columns: repeat(5, 1fr);
  gap: 30px;
  justify-content: center;
  margin: 0 auto;
}

.card-container {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  padding: 10px;
}

.parking-card {
  width: 300px;
  height: 150px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 15px;
  box-sizing: border-box;
  background: linear-gradient(135deg, #ff4081, #81d4fa);
  border-radius: 10px;
  box-shadow: 0px 4px 20px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  color: #fff;
}

.parking-card:hover {
  transform: translateY(-10px);
  box-shadow: 0px 8px 30px rgba(0, 0, 0, 0.2);
}

.dialog-footer {
  text-align: right;
}

.el-dialog {
  width: 60%;
  max-width: 600px;
  min-width: 300px;
  padding: 20px;
  background: linear-gradient(135deg, #81d4fa, #ff4081);
  border-radius: 10px;
  box-shadow: 0px 4px 20px rgba(0, 0, 0, 0.1);
}

.el-form {
  background: #ffffff;
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0px 4px 20px rgba(0, 0, 0, 0.1);
}

.el-form-item {
  margin-bottom: 20px;
}

.el-form-item__label {
  font-weight: bold;
  color: #333;
}

.el-input__inner,
.el-select .el-input__inner {
  border-radius: 5px;
  padding: 10px;
  transition: all 0.3s ease;
  background-color: #f9f9f9;
  border: 1px solid #ccc;
}

.el-input__inner:focus,
.el-select .el-input__inner:focus {
  border-color: #ff4081;
  box-shadow: 0 0 5px #ff4081;
  background-color: #ffffff;
}

.el-button {
  border-radius: 5px;
  padding: 10px 20px;
  background: linear-gradient(135deg, #ff4081, #81d4fa);
  color: #fff;
  transition: all 0.3s ease;
}

.el-button:hover {
  background: linear-gradient(135deg, #81d4fa, #ff4081);
  box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.2);
}

.dialog-footer {
  text-align: right;
  padding: 10px 20px;
  background: #ffffff;
  border-top: 1px solid #eee;
  border-bottom-left-radius: 10px;
  border-bottom-right-radius: 10px;
}
:deep(.el-collapse-item__header){
  font-size: large;
  padding-left: 2%;
}
:deep(.el-tabs--border-card>.el-tabs__content){
  padding: 10px;
}
:deep(.el-tabs--border-card>.el-tabs__header .el-tabs__item){
  font-size: large;
  font-weight:bold;
}
</style>